﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class EditBD : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {

            Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }

            else
            {
                if (!IsPostBack)
                {
                    if (Request["id"] != null)
                        if (Request["id"] == "-1")
                        {
                            this.SaveButton.Text = "添加(Add)";
                            this.CNTitleLabel.Text = "添加PU";
                            this.ENTitleLabel.Text = "Add A PU";
                            BaseFunction.BindBGDropDownList(this.BGCodeDropDownList, "", true);

                        }
                        else
                        {
                            this.SaveButton.Text = "保存(Save)";
                            this.CNTitleLabel.Text = "编辑PU信息";
                            this.ENTitleLabel.Text = "Edit PU Information";
                            this.LoadData();
                        }

                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                }
            }
        }

        protected void LoadData()
        {
            try
            {
                int BDID = int.Parse(Request["id"].ToString());

                BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];
                BEGDC.PreContract.DAL.BD bd = iBD.GetBD(BDID);

                this.CodeTextBox.Text = bd.BDCode;
                this.SortNumberTextBox.Text = bd.SortNumber.ToString();
                this.ActiveCheckBox.Checked = bd.Active;

                //Joan 2008-10-20 date label format
                DateTime date;
                if (bd.CreateDate != Nullables.NullableDateTime.Default)
                {
                    date = System.Convert.ToDateTime(bd.CreateDate.Value);
                    this.CreateDateLabel.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                }

                if (bd.UpdateDate != Nullables.NullableDateTime.Default)
                {
                    date = System.Convert.ToDateTime(bd.UpdateDate.Value);
                    this.UpdateDateLabel.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                }

                //this.CreateDateLabel.Text = bd.CreateDate.ToString();
                //this.UpdateDateLabel.Text = bd.UpdateDate.ToString();


                //End

                if (bd.BDDescription == null)
                    this.DesCriptionTextBox.Text = String.Empty;
                else
                this.DesCriptionTextBox.Text = bd.BDDescription.ToString();

                BaseFunction.BindBGDropDownList(this.BGCodeDropDownList, "", true);
                this.BGCodeDropDownList.SelectedValue = Convert.ToString(bd.BGID.BGID) ;
            }

            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }

        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {
                int BDID = int.Parse(Request["id"].ToString());
                bool flag = true;

                if (this.IsBDCodeExist(this.CodeTextBox.Text.Trim(), BDID))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.ExistedRecord) + "')</script>");
                    return;
                }

                if (this.BGCodeDropDownList.SelectedIndex == 0)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" +BaseFunction.ChangeAlertMessage(AlertMessage.NotChooseBG)+ "')</script>");
                    return;
                }

                if (this.IsSortNumberExist(this.SortNumberTextBox.Text.Trim(), BDID))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.HasSortNumber) + "')</script>");
                    return;
                }

                BEGDC.PreContract.DAL.BG bg = new BEGDC.PreContract.DAL.BG();
                bg.BGID = Convert.ToInt32(this.BGCodeDropDownList.SelectedValue);

                if (BDID == -1)
                {
                    BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];
                    int numflag = iBD.AddBD(this.CodeTextBox.Text.Trim(), Convert.ToInt32(this.SortNumberTextBox.Text.Trim()),DateTime.Now, this.ActiveCheckBox.Checked,
                        String.Empty,bg,DateTime.Now,this.DesCriptionTextBox.Text); 
                    if (numflag == 0)
                        flag = false;
                }
                else
                {
                    BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];
                    BEGDC.PreContract.DAL.BD bd = iBD.GetBD(BDID);

                    flag = iBD.UpdateBD(BDID,this.CodeTextBox.Text.Trim(), Convert.ToInt32(this.SortNumberTextBox.Text.Trim()),bd.CreateDate,
                        this.ActiveCheckBox.Checked, String.Empty, bg, DateTime.Now, this.DesCriptionTextBox.Text);
                }

                if (!flag)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.ExistedRecord) + "')</script>");
                    return;
                }
                this.Response.Write("<script language='javascript'>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "');var arr=new Array(); arr[0]=0; window.returnValue=arr;window.close();</script>");

            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
        }

        protected bool IsBDCodeExist(string bdCode, int bdID)
        {
            string condition = String.Format("BDCode = '{0}'", bdCode);

            if (bdID != -1)
                condition += String.Format(" AND BDID <> {0}", bdID);

            BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];

            DataTable dt = iBD.GetBD_DataTable(condition);

            if (dt.Rows.Count == 0)
                return false;
            else
                return true;
        }

        protected bool IsSortNumberExist(string sortNumber, int bdID)
        {
            string condition = String.Format("SortNumber = '{0}'", sortNumber);

            if (bdID != -1)
                condition += String.Format(" AND BDID <> {0}", bdID);

            BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];

            DataTable dt = iBD.GetBD_DataTable(condition);

            if (dt.Rows.Count == 0)
                return false;
            else
                return true;
        }
    }
}
